/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tubes5progin.client.folderstructure;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import tools.directoryHelper;

/**
 *
 * @author Rezha
 */
public class filesystem implements Serializable {
    //memiliki parent direktory

    public File parentDir;
    //memiliki list of file
    public List<File> files = new LinkedList<File>();
    //memiliki list of directory
    public List<filesystem> directory = new LinkedList<filesystem>();

    //constructor
    public filesystem(String parentFolderName) {
        this.parentDir = new File(parentFolderName);
    }

    public filesystem(filesystem fs){
        this.parentDir = fs.parentDir;
        this.files.addAll(fs.files);
        this.directory.addAll(fs.directory);
    }

    public filesystem(File f) {
        this.parentDir = f;
    }

    public filesystem() {
        
    }

    public void addFiles(File f) {
        this.files.add(f);
    }

    public void addDirectory(filesystem f) {
        this.directory.add(f);
    }

    public void save(File f) {
        FileOutputStream fos = null;
        {
            ObjectOutputStream obs = null;
            try {
                fos = new FileOutputStream(f);
                obs = new ObjectOutputStream(fos);
                obs.writeObject(this);
            } catch (IOException ex) {
                Logger.getLogger(filesystem.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(filesystem.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    obs.close();
                } catch (IOException ex) {
                    Logger.getLogger(filesystem.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void load(File f) {
        FileInputStream f_in = null;
        {
            ObjectInputStream obj_in = null;
            try {
                f_in = new FileInputStream(f);
                obj_in = new ObjectInputStream(f_in);
                Object obj = obj_in.readObject();
                if (obj instanceof filesystem) {
                    this.directory = ((filesystem)obj).directory;
                    this.files = ((filesystem)obj).files;
                    this.parentDir = ((filesystem)obj).parentDir;
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(filesystem.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(filesystem.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    f_in.close();
                } catch (IOException ex) {
                    Logger.getLogger(filesystem.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    obj_in.close();
                } catch (IOException ex) {
                    Logger.getLogger(filesystem.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
